package com.cpecc.dao.system;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cpecc.model.system.WuZiLB;

@Repository
public class WuZiLBDao {

	@Resource
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@SuppressWarnings("unchecked")
	public List<WuZiLB> getAllWuZiLB(){
		String hql="from WuZiLB";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<WuZiLB> getAllWuZiLB1(){
		String hql="from WuZiLB t where t.cengJi=1";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}
	
	public void save(WuZiLB wuZiLB){
		Session session=sessionFactory.getCurrentSession();
		session.save(wuZiLB);
		session.flush();
	}
	
	public boolean cover(WuZiLB wuZiLB){
		Session session=sessionFactory.getCurrentSession();
		String hql="update WuZiLB u set u.leibieMC=?,u.miaoShu=?,u.leiBieJC=? where u.id=?";
		Query query=session.createQuery(hql);
		query.setString(0, wuZiLB.getLeibieMC());
		query.setString(1, wuZiLB.getMiaoShu());
		query.setString(2, wuZiLB.getLeiBieJC());
		query.setString(3, wuZiLB.getId());
		session.flush();
		return query.executeUpdate()>0;
	}
	
	public WuZiLB get(String id){
		return (WuZiLB) sessionFactory.getCurrentSession().get(WuZiLB.class, id);
	}
	
	public void delete(WuZiLB wuZiLB){
		Session session=sessionFactory.getCurrentSession();
		session.delete(wuZiLB);
		session.flush();
	}
	
	@SuppressWarnings("unchecked")
	public List<WuZiLB> getAllWuZiLB2(String id){
		String hql="from WuZiLB t where t.cengJi=2 and t.parent.id=?";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, id);
		return query.list();
	}
	
	public WuZiLB getByWuZiLBMC(String wuZiLBMC){
		String hql="from WuZiLB t where t.leibieMC=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, wuZiLBMC);
		return (WuZiLB) query.list().get(0);
	}
}
